package com.hyt.it.ogt.kq.main.controller.govdata;


import java.util.concurrent.ExecutionException;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.common.enums.ProgressEnum;
import com.hyt.it.ogt.kq.common.gov.annotation.Progress;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.service.bm.service.IProjectExamService;
import com.hyt.it.ogt.kq.service.model.vo.candidateSubject.BmTaskData;
import com.hyt.loginfo.model.User;
import com.hyt.loginfo.service.IBaseLoginService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;

/**
 * 考务绑定报名
 */
@Api(tags = "300.考务任务绑定报名", value = "考务任务绑定报名")
@ApiSort(value = 300)
@RestController
@Slf4j
@RequestMapping("/kw/govdata")
public class KwBingingBmController {

    @Autowired
    private IBaseLoginService baseLoginService;
    
    @Autowired
    private IProjectExamService projectExamService;


    @ApiOperation(value = "1.1.1 绑定报名拉取报名数据", notes = "20000: 成功；61011101：获取当前登录人信息失败, 61011102: 考务绑定报名查询考生信息异常,61011103: 正在同步数据，请稍后再试")
    @ApiOperationSort(value = 1)
    @ApiImplicitParams({
        @ApiImplicitParam(
            name = "taskId", value = "考务任务id", dataType = "String", paramType = "query",  required = true
        ),
        @ApiImplicitParam(
            name = "projectId", value = "报名项目id", dataType = "String", paramType = "query",  required = true
        )
    })
    @GetMapping("bindingbmdata")
    @Progress(name = ProgressEnum.KW_BINDING_BM_DATA)
    public Object bindingbmdata(String taskId, String projectId){
        log.info("# 考务绑定报名 taskId:{}, projectId:{}", taskId, projectId);
        User user = baseLoginService.getSysUser();
        if(null == user || StringUtils.isEmpty(user.getId())) {
            return ApiResponse.<Boolean>builder().code(61011101).data(Boolean.FALSE).build();
        }
        BmTaskData bmTaskData = new  BmTaskData();
        try {
            bmTaskData = projectExamService.bindProjectExam(user.getId(), taskId, projectId);
        } catch (KqException e) {
            log.error("## KqException考务绑定报名查询考生信息异常", e);
            return ApiResponse.<Boolean> builder().code(e.getErrCode()).data(Boolean.FALSE).build();
        } catch (ExecutionException e) {
            log.error("## ExecutionException考务绑定报名查询考生信息异常", e);
            Exception exception = (Exception) e.getCause();
            if(exception instanceof KqException) {
                KqException exception2 = (KqException) exception;
                return ApiResponse.<Boolean> builder().code(exception2.getErrCode()).data(Boolean.FALSE).build();
            }
            return ApiResponse.<Boolean> builder().code(61011103).data(Boolean.FALSE).build();
        } catch (Exception e) {
            log.error("修改锁定状态异常：", e);
            return ApiResponse.builder()
                    .code(ResponseCode.ERROR_OVERVIEW_BIND.getCode())
                    .build();
        }
        if(null == bmTaskData){
            return ApiResponse.<Boolean>builder().code(20000).data(Boolean.TRUE).build();
        }
        if(CollectionUtils.isEmpty(bmTaskData.getCandidateList())){
            return ApiResponse.<Boolean>builder().code(20000).data(Boolean.TRUE).build();
        }
        return ApiResponse.<Boolean>builder().code(20000).data(Boolean.TRUE).build();
    }

}
